﻿using CSM_SQL;
using CSM_SQL.BLL;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace CommunitySuperMarketManager.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Goods()
        {
            var e = (Emp)HttpContext.Session["CurrUser"];
            if (e != null)
            {
                ViewBag.CurrUser = e;
                if (e.Auth > 0)
                    return View();
                else
                    return RedirectToAction("PersonalOrders", "PersonalMsg");
            }
            return RedirectToAction("Login", "Login");
        }

        //上传图片
        public void Upload()
        {
            var file = Request.Files[0];
            var path = Server.MapPath("~/GoodsImg");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            Guid gid = Guid.NewGuid();
            var fname = gid + "_" + file.FileName.Split('\\').Last();
            path += "\\" + fname;
            file.SaveAs(path);
            var a = new { fname = fname };
            HttpContext.Response.Write(JsonConvert.SerializeObject(a));
            HttpContext.Response.End();
        }

        public void AllGoods()
        {
            
            var pageNamber = Convert.ToInt32(Request.Params["page"]);
            var pageSize = Convert.ToInt32(Request.Params["rows"]);
           
            GoodsBLL goodsBLL = new GoodsBLL();
            List<Goods> list = goodsBLL.All();
            Hashtable ht = new Hashtable
            {
               { "total", list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Goods>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void Add()
        {
            Goods item = new Goods
            {
                Name = Request.Form["Name"],
                Type = Convert.ToInt32(Request.Form["Type"]),
                Detail = Request.Form["Detail"],
                Price = Convert.ToDecimal(Request.Form["price"]),
                Stock = Convert.ToInt32(Request.Form["Stock"]),
                Remark = Request.Form["Remark"],
                Weight = Convert.ToInt32(Request.Form["Weight"]),
                Img = Request.Form["Img"]
            };

            GoodsBLL gb = new GoodsBLL();

            if (gb.Add(item))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
            
        }

        public void Edit(int ID)
        {
            Goods item = new Goods
            {
                G_ID = ID,
                Name = Request.Form["Name"],
                Type = Convert.ToInt32(Request.Form["Type"]),
                Detail = Request.Form["Detail"],
                Price = Convert.ToDecimal(Request.Form["price"]),
                Stock = Convert.ToInt32(Request.Form["Stock"]),
                Remark = Request.Form["Remark"],
                Weight = Convert.ToInt32(Request.Form["Weight"]),
                Img = Request.Form["Img"]
            };

            GoodsBLL gb = new GoodsBLL();

            if (gb.Edit(item))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }

        public void Delete(int ID)
        {
            GoodsBLL gb = new GoodsBLL();

            if (gb.Delete(ID))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }

        public void BatDel()
        {
            GoodsBLL gb = new GoodsBLL();
            int result = 0;
            var IDs = Request.Params["IDs"].Split(',');
            foreach (var id in IDs)
            {
                if (gb.Delete(Convert.ToInt32(id)))
                    result++;
            }
            HttpContext.Response.Write(result);
            HttpContext.Response.End();
        }

        public void Search()
        {
            var pageNamber = Convert.ToInt32(Request.Params["pageNumber"]);
            var pageSize = Convert.ToInt32(Request.Params["pageSize"]);
            var type = Convert.ToInt32(Request.Params["type"]);
            var text = Request.Params["text"];

            GoodsBLL goodsBLL = new GoodsBLL();
            List<Goods> list;
            if (type != -1)
                list = (from g in goodsBLL.All() where g.Type == type select g).ToList();
            else
                list = goodsBLL.All();

            if (text.Length > 0)
                list = (from g in list where g.Name.Contains(text) || g.Detail.Contains(text) || g.Remark.Contains(text) || g.Price.ToString().Contains(text) || g.Stock.ToString().Contains(text) select g).ToList();

            Hashtable ht = new Hashtable
            {
               { "total",list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Goods>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void InStore(int id)
        {
            var num = Convert.ToInt16(Request.Params["num"]);
            GoodsBLL gb = new GoodsBLL();
            if (gb.InStore(id, num))
                HttpContext.Response.Write(0);
            HttpContext.Response.End();
        }
    }
}